# -*- encoding: utf-8 -*-
'''
@File    :   main.py
@Time    :   2020/10/21 16:12:09
@Author  :   Wicos 
@Version :   1.0
@Contact :   wicos@wicos.cn
@Blog    :   https://www.wicos.me
'''

# here put the import lib
from fastapi import FastAPI,UploadFile,File
from fastapi.middleware.cors import CORSMiddleware
from starlette.templating import Jinja2Templates
from starlette.staticfiles import StaticFiles
from typing import List
from starlette.requests import Request
import json
from pydantic import BaseModel
import time


app = FastAPI()
origins = [
    "*"
]

templates = Jinja2Templates(directory="templates")
app.mount('/static', StaticFiles(directory='static'), name='static')
templates.env.variable_end_string = ']}' # 解决冲突
templates.env.variable_start_string = '{['
app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"]
)

class Trans(BaseModel):
    longitude:str
    latitude:str

class Add(BaseModel):
    data:dict

class ZIPPIC(BaseModel):
    filename:str
    quality:int


@app.get("/")
async def index(request: Request):
    return templates.TemplateResponse("index.html", {'request': request})

def read_json(jsonfile):
    with open(jsonfile,'rb') as fp:
        a = json.load(fp)
    return a

def write_json(jsonfile,data):
    with open(jsonfile,'w') as fp:
        fp.write(json.dumps(data,indent=4))
        
"""
@app.post('/trans')
async def trans(item:Trans):
    users_data = item.dict()
    trans_jw = amaps.trans(users_data['longitude'],users_data['latitude'])
    return trans_jw
"""